<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Users;
use think\Db;
use app\common\model\PrepareQueue as PrepareQueueModel;
use think\Model;

/**
 * 技师排班控制器
 * @package app\index\controller
 */
class Scheduling extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取所有为排班的技师
     */
    public function getEngineer(){
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_users')
                ->field('id,work_number')
                ->where(['role'=>'3','work_type'=>'0','store_id'=>$userInfo['store_id']])
                ->select();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取技师排班分类
     */
    public function getScheduling(){
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_scheduling_type')
                ->field('id,title,start_time,end_time,sort')
                ->where(['store_id'=>$userInfo['store_id']])
                ->order('sort  asc')
                ->select();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 添加技师排班
     */
    public function addEngineer(){
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(!isset($getData['scheduling_id']) || empty($getData['scheduling_id'])){
                $this->result('缺少参数', '', 204);
            }
            if(!isset($getData['engineer_id']) || empty($getData['engineer_id'])){
                $this->result('缺少参数', '', 204);
            }
            $engineerRes = [];
            $engineerIds = explode(',',$getData['engineer_id']);
            $sortRes = Db::name('qt_scheduling_queue')
                ->where(['store_id'=>$userInfo['store_id'],'type_id'=>$getData['scheduling_id']])
                ->order('sort desc')
                ->limit('1')
                ->find();
            if(empty($sortRes)){
                $sort = 1;
            }else{
                $sort = $sortRes['sort'] + 1;
            }
            foreach ($engineerIds as $key=>$value){
                $engineerRes[$key]['store_id'] = $userInfo['store_id'];
                $engineerRes[$key]['engineer_id'] = $value;
                $engineerRes[$key]['sort'] = $sort;
                $engineerRes[$key]['type_id'] = $getData['scheduling_id'];
                $engineerRes[$key]['created_time'] = time();
                $sort ++ ;
            }
            $insert = Db::name('qt_scheduling_queue')->insertAll($engineerRes);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败，请稍后再试', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加班次
     */
    public function addScheduling()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData= $this->request->post();
            $validate = new \app\api\validate\Scheduling();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['store_id'] = $userInfo['store_id'];
            $getData['created_time'] = time();
            $insert = Db::name('qt_scheduling_type')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败，请稍后再试', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改班次
     */
    public function saveScheduling()
    {
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new \app\api\validate\Scheduling();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['created_time'] = time();
            $insert = Db::name('qt_scheduling_type')->where(['id'=>$getData['id']])->update($getData);
            if($insert){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败，请稍后再试', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改班次
     */
    public function editScheduling()
    {
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_scheduling_type')->where(['id'=>$getData['id']])->find();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 换班
     */
    public function exchange(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $userInfo = $this->userInfo;
            if(!isset($getData['old_scheduling_id']) || empty($getData['old_scheduling_id'])){
                $this->result('缺少参数', '', 204);
            }
            if(!isset($getData['new_scheduling_id']) || empty($getData['new_scheduling_id'])){
                $this->result('缺少参数', '', 204);
            }
            //老班次
            $oldUserRes = Db::name('qt_users')
                ->where(['store_id'=>$userInfo['store_id'],'work_type'=>$getData['old_scheduling_id'],'role'=>'3'])
                ->select();
            //新班次
            $newUserRes = Db::name('qt_users')
                ->where(['store_id'=>$userInfo['store_id'],'work_type'=>$getData['new_scheduling_id'],'role'=>'3'])
                ->select();
            static $insertOld = [];
            $sort  = 1;
            $i = 1;
            //老的改新的
            $insertOld = [];
            foreach ($oldUserRes as $key=>$value){
                Db::name('qt_users')->where('id',$value['id'])->update(['work_type'=>$getData['new_scheduling_id']]);
                $insertOld[$i]['store_id'] = $userInfo['store_id'];
                $insertOld[$i]['engineer_id'] = $value['id'];
                $insertOld[$i]['sort'] = $sort;
                $insertOld[$i]['type_id'] = $getData['new_scheduling_id'];
                $insertOld[$i]['created_time']= time();
                $i++;
                $sort ++;
            }
            static $insertOld = [];
            $sort  = 1;
            $j = 1;
            //新的改老的
            $insertNew = [];
            if(!empty($newUserRes)){
                foreach ($newUserRes as $key=>$value){
                    Db::name('qt_users')->where('id',$value['id'])->update(['work_type'=>$getData['old_scheduling_id']]);
                    $insertNew[$j]['store_id'] = $userInfo['store_id'];
                    $insertNew[$j]['engineer_id'] = $value['id'];
                    $insertNew[$j]['sort'] = $sort;
                    $insertNew[$j]['type_id'] = $getData['old_scheduling_id'];
                    $insertNew[$j]['created_time']= time();
                    $i++;
                    $sort ++;
                }
            }
            try{
                Db::startTrans();
                Db::name('qt_scheduling_queue')
                    ->where(['type_id'=>$getData['old_scheduling_id'],'store_id'=>$userInfo['store_id']])
                    ->delete();
                Db::name('qt_scheduling_queue')->insertAll($insertOld);
                Db::name('qt_scheduling_queue')->insertAll($insertNew);
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('系统繁忙，请稍后再试','',204);
            }
            $this->result('换班成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
